/******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZEBUFF 80
#define CARDINAL(Array) ((sizeof(Array)/sizeof(*Array)))

typedef struct _Descriptor Descriptor ;

struct _Descriptor {
   const char name[SIZEBUFF] ;
   const char number[SIZEBUFF] ;
} ;

static Descriptor TheMonths[] = {
   {"Jan" , "01"},
   {"Feb" , "02"},
   {"Mar" , "03"},
   {"Apr" , "04"},
   {"May" , "05"},
   {"Jun" , "06"},
   {"Jul" , "07"},
   {"Aug" , "08"},
   {"Sep" , "09"},
   {"Oct" , "10"},
   {"Nov" , "11"},
   {"Dec" , "12"},
   {"Def" , "13"}
} ;

/******************************************************************************/
/* Indice                                                                     */
/******************************************************************************/
static int Indice(const char *Mmm)
{
   int i ;

   for (i = 0 ; i < CARDINAL(TheMonths) - 1 ; i++)
      if (!strcmp(Mmm, TheMonths[i].name))
         return i ;

   return i ;
}

/******************************************************************************/
/* compildate                                                                 */
/*                                                                            */
/*  -  saves the compilation date in the integer number pointed by date       */
/*  -  *date is a integer number of the form YYMMDD where each two digits     */
/*     represents the following :                                             */
/*        YY : the second digits of the year                                  */
/*        MM : the month of the year (01 - 12)                                */
/*        DD : the day of the month  (01 - 31)                                */
/*                                                                            */
/******************************************************************************/
void FortranName(COMPILDATE,compildate) (int *date)
{
   int  indice ;
   char _day[SIZEBUFF], _month[SIZEBUFF], _year[SIZEBUFF], _date[SIZEBUFF] ;

   sscanf(__DATE__,"%s%s%s", _month, _day, _year) ;

   if (strlen(_day) == 1) {
      _day[2] = '\0' ;
      _day[1] = _day[0] ;
      _day[0] = '0' ;
   }
      
#ifdef TEST_COMPILATION
   printf("%s\n", __DATE__) ; // hyvq
   printf("month = %s\n", _month) ;
   printf("day   = %s\n", _day) ;
   printf("year  = %s\n", _year) ;
#endif /* TEST_COMPILATION */

   if ((indice = Indice(_month)) == CARDINAL(TheMonths)) {
      fprintf(stderr," *** WARNING *** __FILE__ : THE MONTH IS UNKNOWN\n") ;
   }
   
   else {
      sprintf(_date,"%s%s%s\0", _year, TheMonths[indice].number, _day) ;

#ifdef TEST_COMPILATION
   printf("date  = %s\n", _date) ;
#endif /* TEST_COMPILATION */

      *date = atoi(_date) ;
   }
}


/******************************************************************************/
/* LOCAL TEST                                                                 */
/******************************************************************************/
#ifdef TEST_COMPILATION

int main(int argc, char *argv[])
{
   int date ;

   compilation_date(&date) ;
   printf("instal = %d\n", date) ;
   return 0 ;
}

#endif /* TEST_COMPILATION */
